

#include "wxsVector.h"
#include <wxwidgets/wxsitemresdata.h>


namespace
{


    #include "images/vector16.xpm"
    #include "images/vector32.xpm"

    wxsRegisterItem<wxsVector> Reg(
        _T("mpVector"),                
        wxsTWidget,                    
        _T("wxWindows"),               
        _T("Ron Collins"),             
        _T("rcoll@theriver.com"),      
        _T(""),                        
        _T("MathPlot"),                
        70,                            
        _T("Vector"),                  
        wxsCPP,                        
        1, 0,                          
        wxBitmap(vector32_xpm),        
        wxBitmap(vector16_xpm),        
        false);                        


    WXS_ST_BEGIN(wxsVectorStyles,_T(""))
        WXS_ST_CATEGORY("mpVector")
        WXS_ST(wxST_NO_AUTORESIZE)
        WXS_ST(wxALIGN_LEFT)
        WXS_ST(wxALIGN_RIGHT)
        WXS_ST(wxALIGN_CENTRE)
        WXS_ST_DEFAULTS()
    WXS_ST_END()

    WXS_EV_BEGIN(wxsVectorEvents)
    WXS_EV_END()

}


wxsVector::wxsVector(wxsItemResData* Data):
    wxsWidget(
        Data,
        &Reg.Info,
        wxsVectorEvents,
        wxsVectorStyles) {
    mLabel      = _("Vector");
    mAlign      = mpALIGN_NE;
    mContinuous = true;

    mXYData.Clear();
    mXYData.Add(_T("! Enter X and Y data here, as pairs of numbers separated by commas."));
    mXYData.Add(_T("! For example:"));
    mXYData.Add(_T("!   1.5, 2.3"));
    mXYData.Add(_T("!   100, 300"));
    mXYData.Add(_T("! Blank lines and lines starting with \"!\" will be ignored."));
    mXYData.Add(_T("! All white-space will be ignored."));
    mXYData.Add(_T(""));

}



void wxsVector::OnBuildCreatingCode() {
int         i,n;
wxString    vname;
wxString    pname;
wxString    cname;
wxString    fname;
wxString    xname;
wxString    yname;
wxString    dtext;
wxString    s;


    if (GetLanguage() != wxsCPP) wxsCodeMarks::Unknown(_T("wxsVector::OnBuildCreatingCode"),GetLanguage());


    vname = GetVarName();
    pname = GetParent()->GetVarName();
    cname = vname + _("_PEN");
    fname = vname + _("_FONT");
    xname = vname + _("_X");
    yname = vname + _("_Y");


    AddHeader(_T("<mathplot.h>"),GetInfo().ClassName,hfInPCH);


    Codef(_T("%s = new mpFXYVector(_(\"%s\"), %d);\n"), vname.wx_str(), mLabel.wx_str(), mAlign);


    dtext = mPenColour.BuildCode(GetCoderContext());
    if (dtext.Len() > 0) {
        Codef(_T("wxPen   %s(%s);\n"), cname.wx_str(), dtext.wx_str());
        Codef(_T("%s->SetPen(%s);\n"), vname.wx_str(), cname.wx_str());
    };


    dtext = mPenFont.BuildFontCode(fname, GetCoderContext());
    if (dtext.Len() > 0) {
        Codef(_T("%s"), dtext.wx_str());
        Codef(_T("%s->SetFont(%s);\n"), vname.wx_str(), fname.wx_str());
    };


    dtext = _("std::vector<double>  ") + xname + _(";");
    AddDeclaration(dtext);
    dtext = _("std::vector<double>  ") + yname + _(";");
    AddDeclaration(dtext);


    ParseXY();
    n = mXs.GetCount();
    if (n > 0) {
        for(i=0; i<n; i++) {
            Codef(_T("%s.push_back(%s);   %s.push_back(%s);\n"), xname.wx_str(), mXs[i].wx_str(),
                                                                 yname.wx_str(), mYs[i].wx_str());
        };

        Codef(_T("%ASetData(%s, %s);\n"), xname.wx_str(), yname.wx_str());
    };


    Codef(_T("%ASetContinuity(%b);\n"), mContinuous);


    if ((GetPropertiesFlags() & flHidden) && GetBaseProps()->m_Hidden) n = 0;       
    else Codef(_T("%s->AddLayer(%s);\n"), pname.wx_str(), vname.wx_str());


}



void    wxsVector::ParseXY(void) {
int         i,n;
int         j;
double      d;
wxString    s,t;


    mXs.Clear();
    mYs.Clear();
    mXf.clear();
    mYf.clear();


    n = mXYData.GetCount();
    for(i=0; i<n; i++) {
        s = mXYData[i];


        s.Trim(true);
        s.Trim(false);
        if (s.IsEmpty()) continue;
        if (s[0] == '!') continue;


        j = s.Find(_T(","));
        if (j != wxNOT_FOUND) {
            t = s.Left(j);
            s.Remove(0, j+1);
        }
        else {
            t = s;
            s = _T("");
        };

        t.Trim(true);
        t.Trim(false);
        t.ToDouble(&d);
        mXs.Add(t);
        mXf.push_back(d);


        t = s;
        t.Trim(true);
        t.Trim(false);
        t.ToDouble(&d);
        mYs.Add(t);
        mYf.push_back(d);
    };



    n = mXs.GetCount();
    if (n > 0) {
        s = mXs[n-1];
        mXs.Add(s);
        s = mYs[n-1];
        mYs.Add(s);

        d = mXf[n-1];
        mXf.push_back(d);
        d = mYf[n-1];
        mYf.push_back(d);
    };
}


wxObject* wxsVector::OnBuildPreview(wxWindow* Parent, long Flags) {
int             n;
wxStaticText    *Preview;
mpFXYVector     *vec;
mpWindow        *mp;
wxPen           pen;
wxColour        cc;
wxFont          ff;
bool            hide;


    if (! Parent->IsKindOf(CLASSINFO(mpWindow))) return NULL;
    mp = (mpWindow *) Parent;


    hide = ((Flags & pfExact) && (GetPropertiesFlags() & flHidden) && GetBaseProps()->m_Hidden);


    Preview = new wxStaticText(Parent, GetId(), mLabel, Pos(Parent), Size(Parent), (wxSUNKEN_BORDER|Style()));
    Preview->SetForegroundColour(wxColour(255,255,255));
    Preview->SetBackgroundColour(wxColour(0,0,128));
    SetupWindow(Preview,Flags);
    if (Flags & pfExact) Preview->Hide();


    vec = new mpFXYVector(mLabel, mAlign);


    cc = mPenColour.GetColour();
    if (cc.IsOk()) pen.SetColour(cc);
    vec->SetPen(pen);


    ff = mPenFont.BuildFont();
    vec->SetFont(ff);


    if (cc.IsOk()) Preview->SetBackgroundColour(cc);
    Preview->SetFont(ff);


    ParseXY();
    n = mXs.GetCount();
    if (n > 0) {
        vec->SetData(mXf, mYf);
    };


    vec->SetContinuity(mContinuous);


    if (! hide) mp->AddLayer(vec);


    return Preview;
}



void wxsVector::OnBuildDeclarationsCode() {

    if (GetLanguage() == wxsCPP) {
        AddDeclaration(_T("mpFXYVector   *") + GetVarName() + _T(";"));
    }
    else {
        wxsCodeMarks::Unknown(_T("wxsVector::OnBuildDeclarationsCode"),GetLanguage());
    };
}





void wxsVector::OnEnumWidgetProperties(cb_unused long Flags) {
static const long    AlignValues[] = {    mpALIGN_NE,       mpALIGN_NW,       mpALIGN_SW,       mpALIGN_SE,    0};
static const wxChar* AlignNames[]  = {_T("mpALIGN_NE"), _T("mpALIGN_NW"), _T("mpALIGN_SW"), _T("mpALIGN_SE"),  0};

    WXS_SHORT_STRING(wxsVector, mLabel,      _("Label Text"),      _("mLabelText"),  _("Label"), true);
    WXS_ENUM(        wxsVector, mAlign,      _("Label Alignment"), _("mAlign"),      AlignValues, AlignNames, mpALIGN_NE);
    WXS_BOOL(        wxsVector, mContinuous, _("Continuous Line"), _("mContinuous"), true);
    WXS_COLOUR(      wxsVector, mPenColour,  _("Pen Colour"),      _("mPenColour"));
    WXS_FONT(        wxsVector, mPenFont,    _("Pen Font"),        _("mPenFont"));
    WXS_ARRAYSTRING( wxsVector, mXYData,     _("X,Y Data"),        _("mXYData"),     _("nums"));

}
